package indi.zhifa.recipe.bailan.busy.auth.service.impl;
import com.alibaba.fastjson2.JSONObject;
import indi.zhifa.recipe.bailan.framework.auth.impl.dao.service.IAuthNodeDbService;
import indi.zhifa.recipe.bailan.framework.auth.impl.entity.po.AuthNodeEntity;
import indi.zhifa.recipe.bailan.busy.auth.dao.service.ICompanyDbService;
import indi.zhifa.recipe.bailan.busy.auth.dao.service.IDeptDbService;
import indi.zhifa.recipe.bailan.busy.auth.dao.service.IDeviceGroupDbService;
import indi.zhifa.recipe.bailan.busy.auth.entity.dto.department.DepartmentCreateDto;
import indi.zhifa.recipe.bailan.busy.auth.entity.dto.department.DepartmentEditDto;
import indi.zhifa.recipe.bailan.busy.auth.entity.po.CompanyEntity;
import indi.zhifa.recipe.bailan.busy.auth.entity.po.DeptEntity;
import indi.zhifa.recipe.bailan.busy.auth.entity.po.DeviceGroupEntity;
import indi.zhifa.recipe.bailan.busy.auth.service.IAuthNodeService;
import indi.zhifa.recipe.bailan.framework.auth.dao.IBaseAuthNodeRelevantDao;
import indi.zhifa.recipe.bailan.framework.auth.entity.dto.init.AuthNodeCfg;
import indi.zhifa.recipe.bailan.framework.auth.entity.po.BaseAuthNodeEntity;
import indi.zhifa.recipe.bailan.framework.auth.service.impl.BaseAuthNodeServiceImpl;
import indi.zhifa.recipe.bailan.framework.mysql.utils.DbDtoEntityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class AuthNodeServiceImpl extends BaseAuthNodeServiceImpl implements IAuthNodeService {

    private final IAuthNodeDbService mDepartmentDbService;
    private final ICompanyDbService mCompanyDbService;
    private final IDeviceGroupDbService mDeviceGroupDbService;
    private final IDeptDbService mDeptDbService;


    public AuthNodeServiceImpl(IBaseAuthNodeRelevantDao pDepartmentRelevantDao,
                               IAuthNodeDbService mDepartmentDbService,
                               ICompanyDbService pCompanyDbService,
                               IDeviceGroupDbService pDeviceGroupDbService,
                               IDeptDbService pDeptDbService){
        super(pDepartmentRelevantDao);
        this.mDepartmentDbService = mDepartmentDbService;
        mCompanyDbService = pCompanyDbService;
        mDeviceGroupDbService = pDeviceGroupDbService;
        mDeptDbService = pDeptDbService;
    }

    @Override
    protected void onInit(String pDomain, List<AuthNodeCfg> pAuthNodeCfgList, List<BaseAuthNodeEntity> pSavingBaseAuthNodeEntityList) {
        assert(pAuthNodeCfgList.size() == pSavingBaseAuthNodeEntityList.size());

        List<DeviceGroupEntity> deviceGroupEntityList = new ArrayList<>();
        List<CompanyEntity> companyEntityList = new ArrayList<>();
        List<DeptEntity> deptEntityList = new ArrayList<>();

        for(int i=0;i<pAuthNodeCfgList.size();i++){
            AuthNodeCfg authNodeCfg = pAuthNodeCfgList.get(i);
            if("rt".equals(authNodeCfg.getCode())){
                continue;
            }

            BaseAuthNodeEntity baseAuthNodeEntity = pSavingBaseAuthNodeEntityList.get(i);
            JSONObject jsonObject = authNodeCfg.getCfg();
            switch (pDomain){
                case "zf-mgr":
                    DeviceGroupEntity deviceGroupEntity = DbDtoEntityUtil.createFromDto(jsonObject,DeviceGroupEntity.class);
                    deviceGroupEntity.setAuthId(baseAuthNodeEntity.getId());
                    deviceGroupEntityList.add(deviceGroupEntity);
                    break;
                case "zf-merchant":
                    CompanyEntity companyEntity = DbDtoEntityUtil.createFromDto(jsonObject,CompanyEntity.class);
                    companyEntity.setAuthId(baseAuthNodeEntity.getId());
                    companyEntityList.add(companyEntity);
                    break;
                case "zf-account":
                    DeptEntity deptEntity = DbDtoEntityUtil.createFromDto(jsonObject,DeptEntity.class);
                    deptEntity.setAuthId(baseAuthNodeEntity.getId());
                    deptEntityList.add(deptEntity);
                    break;
            }
        }
        mDeviceGroupDbService.saveBatch(deviceGroupEntityList);
        mCompanyDbService.saveBatch(companyEntityList);
        mDeptDbService.saveBatch(deptEntityList);
    }

    @Override
    protected void onDepCodeChange(BaseAuthNodeEntity pBaseAuthNodeEntity) {

    }

    @Override
    protected void onDepDelete(BaseAuthNodeEntity pBaseAuthNodeEntity) {
        mDeviceGroupDbService.deleteByAuthId(pBaseAuthNodeEntity.getId());
        mCompanyDbService.deleteById(pBaseAuthNodeEntity.getId());
        mDeptDbService.deleteByAuthId(pBaseAuthNodeEntity.getId());
    }


    @Override
    public AuthNodeEntity create(Long pParentId, DepartmentCreateDto pDepartmentCreateDto) {
        AuthNodeEntity authNodeEntity = DbDtoEntityUtil.createFromDto(pDepartmentCreateDto, AuthNodeEntity.class);
        return (AuthNodeEntity)super.create(pParentId, authNodeEntity);
    }

    @Override
    public AuthNodeEntity edit(Long pId, DepartmentEditDto pDepartmentEditDto) {
        AuthNodeEntity orgAuthNodeEntity = mDepartmentDbService.check(pId);
        AuthNodeEntity newAuthNodeEntity = DbDtoEntityUtil.editByDto(orgAuthNodeEntity,pDepartmentEditDto, AuthNodeEntity.class);
        newAuthNodeEntity = mDepartmentDbService.updatePull(newAuthNodeEntity.getId(), newAuthNodeEntity);
        return newAuthNodeEntity;
    }

}
